package cn.campus.service.impl;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import cn.campus.dao.ForumMapper;
import cn.campus.dao.UserMapper;
import cn.campus.pojo.Forum;
import cn.campus.pojo.ForumAndUser;
import cn.campus.service.ForumService;

@Service
public class ForumServiceImpl implements ForumService {

	@Resource
	private ForumMapper forumMapper;

	@Resource
	private UserMapper userMapper;

	@Override
	public int addForum(Forum forum) {
		// TODO Auto-generated method stub
		return forumMapper.insertForum(forum);
	}
	@Override
	public ForumAndUser findAllForums(ForumAndUser forumAndUser) {
		// TODO Auto-generated method stub
		ForumAndUser findAllForums=new ForumAndUser();
		List<Forum> forums = forumMapper.selectAllForums(forumAndUser);
		//防止forums空值报错
		if(forums!=null){
			forumAndUser.setForums(forums);
			findAllForums.setUsers(userMapper.selectAllUsersByForum(forumAndUser));
		}
		//每页的数量
		int count=forumAndUser.getCount();
		findAllForums.setCount(count);
		//设置当前页
		findAllForums.setCurrentPage(forumAndUser.getCurrentPage());
		//查询总数
		int total=forumMapper.selectForumSum(forumAndUser);
		findAllForums.setTotal(total);
		//设置总页数
		findAllForums.setTotalPage((int)Math.ceil(total/count));
		//设置所有帖子数
		findAllForums.setForums(forums);
		return findAllForums;
	}
	@Override
	public Forum findForum(String forum_id) {
		// TODO Auto-generated method stub
		return forumMapper.selectForum(forum_id);
	}

}
